package com.ajun.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ajun.entity.Specification;
import com.ajun.mapper.SpecificationMapper;
import com.ajun.service.ISpecificationService;
import com.ajun.vo.SpecificationVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.util.PageResult;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzj
 * @since 2025-01-09
 */
@Service
public class SpecificationServiceImpl extends ServiceImpl<SpecificationMapper, Specification> implements ISpecificationService {

    //分页查询
    @Override
    public PageResult<Specification> findByPage(Integer page, Integer pageSize) {
        //开始分页
        Page<Specification> page1 = this.page(new Page<>(page,pageSize));
        //封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    //分页+条件查询
    @Override
    public PageResult<Specification> search(Integer page, Integer pageSize, SpecificationVo vo) {
        //封装查询条件
        QueryWrapper<Specification> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getSpecName()), "spec_name", vo.getSpecName());
        //分页
        Page<Specification> page1 = this.page(new Page<>(page, pageSize), wrapper);
        //返回结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    //添加
    @Override
    public boolean add(Specification specification) {
        return this.save(specification);
    }

}
